/**
 * 
 */
package cwinsor.com.leaf_raking;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

/**
 * @author cwinsor
 *
 */
public class Appointment  implements Comparator<Appointment> {

	/*
	 * properties
	 */
	Day myDay;
	StartTime myStartTime;
	//	AmPm myAmPm;

	WorkShift myShift;

	TeamNumber myTeam;

	//	String town;
	String name;
	String shiftInfo;
	String phone;
	String address;
	String sizeOfArea;
	String notes;
	String email;

	/*
	 * debug
	 */
	private int debugSection = 1;
	String dM = this.getClass().getSimpleName() + "----";
	String dF = this.getClass().getSimpleName() + "---- ERROR ---- ";

	/*
	 * addData
	 * take in raw data, check, and internalize
	 */
	public void addData(ArrayList<String> theFieldsObject) {

		// basics
		shiftInfo = theFieldsObject.get(2);
		shiftInfo = shiftInfo.replaceAll(" +", " ");
		name = theFieldsObject.get(6);
		phone = theFieldsObject.get(7);
		address = theFieldsObject.get(8);


		// BHRA-specified Team will be in the Notes - otherwise UNASSIGNED
		if (theFieldsObject.size() > 10) {
			myTeam = TeamNumber.stringContainsTeam(theFieldsObject.get(10).toString(),false);
		} else {
			myTeam = TeamNumber.TEAM_UNASSIGNED;
		}

		// if there is a BHRA-specified Shift/StartTime in the Notes from SuperSaas - that takes precedence
		myDay = null;
		myStartTime = null;
		//		myAmPm = null;
		myShift = null;

		// THE FOLLOWING CODE ALLOWS USER TO FORCE SHIFT/STARTTIME BY PUTTING THIS INFO IN THE APPOINTMENT NOTES - NOT RECOMMENDED
		//	if (theFieldsObject.size() > 10) {
		//		myShift = Shift.stringContainsShift(theFieldsObject.get(10).toString(), false);
		//		myStartTime = StartTime.stringContainsStartTime(theFieldsObject.get(10).toString(), false);
		//		// need both
		//		if ((myShift == null) || (myStartTime == null)) {
		//			myShift = null;
		//			myStartTime = null;
		//		} else {
		//			Msg.note(dM + ": NOTE - a comment in SuperSAAS notes has overridden Shift/StartTime for this appointment "
		//					+ name.toString()
		//					+ " !!!!!!!!!");
		//		}
		//	}

		// get Shift/StartTime from the Start Time in SuperSAAS

		// the input file (from SuperSAAS) has
		// 11/2/2013  8:30:00 AM
		// we first establish appointment information:
		// StartTime
		// AmPm
		// from these we can identify a WorkShift
		if ((myDay == null) || (myStartTime == null) /* || (myAmPm == null) */) {
			/*zona*/
			myDay = Day.fromString(theFieldsObject.get(0));
			myStartTime = StartTime.fromString(theFieldsObject.get(0));
			//			myAmPm = AmPm.fromString(theFieldsObject.get(0));   WorkShift.
			myShift = WorkShift.fromDayStartTime(myDay, myStartTime);
		}

		if (theFieldsObject.size() > 9) {
			sizeOfArea = theFieldsObject.get(9);
		} else {
			sizeOfArea = "";
		}

		if (theFieldsObject.size() > 10) {
			notes = theFieldsObject.get(10).toString();
		} else {
			notes = "";
		}
		if (theFieldsObject.size() > 11) {
			email = theFieldsObject.get(11).toString();
		} else {
			email = "";
		}
	}



	/**
	 * getters
	 */
	Day getDay() {
		return myShift.day();
	}
	StartTime getStartTime() {
		return myStartTime;
	}
	WorkShift getShift() {
		return myShift;
	}

	TeamNumber getTeam() {
		return myTeam;
	}

	String getShiftInfo() {
		return shiftInfo;
	}

	String getName() {
		return name;
	}
	String getPhone() {
		return phone;
	}
	String getAddress() {
		return address;
	}
	String getSizeOfArea() {
		return sizeOfArea;
	}
	String getNotes() {
		return notes;
	}
	String getEmail() {
		return email;
	}

	public String getDuration() {
		if (shiftInfo.contains("1 HOUR")) { return "1 HOUR"; }
		if (shiftInfo.contains("1.5 HOURS")) { return "1.5 HOURS"; }
		if (shiftInfo.contains("2 HOURS")) { return "2 HOURS"; }
		Msg.fatal(dM + ": getDuration - hit the default case with \"" + shiftInfo + "\"");
		return null;
	}

	/*
	 * toStringCommaDelimited
	 * return fields as a comma-delimited string
	 */
	public String toStringCommaDelimitedAllFields() {
		String s = new String();

		s = getDay()
				+ "," + getStartTime().toString()
				+ "," + getShift().toString()
				+ "," + getTeam().toString()
				//				+ "," + getTown().toString()
				+ "," + getName().toString()
				+ "," + getPhone().toString()
				+ "," + getAddress().toString()
				+ "," + getSizeOfArea().toString()
				+ "," + getNotes().toString();
		return s;
	}



	@Override
	/* 
	 * comparator for sorting by start time
	 */
	public int compare(Appointment o1, Appointment o2) {
		return (o1.myStartTime.toIndex() - o2.myStartTime.toIndex());
	}
}
